package cn.cdzdjd.controller;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.cdzdjd.annotation.Log;
import cn.cdzdjd.annotation.Login;
import cn.cdzdjd.constant.LocationRedirect;
import cn.cdzdjd.constant.VideoPlatform;
import cn.cdzdjd.dto.form.UserInputForm;
import cn.cdzdjd.exception.ErrorCode;
import cn.cdzdjd.service.AcfunService;
import cn.cdzdjd.service.AllowURLService;
import cn.cdzdjd.service.BaiduHaoKanService;
import cn.cdzdjd.service.BiliBiliService;
import cn.cdzdjd.service.DianPingService;
import cn.cdzdjd.service.DouyinVideoService;
import cn.cdzdjd.service.DuanYouService;
import cn.cdzdjd.service.HuYaService;
import cn.cdzdjd.service.IXiGuaService;
import cn.cdzdjd.service.JDVideoService;
import cn.cdzdjd.service.KuaiShouVideoService;
import cn.cdzdjd.service.MeiPaiService;
import cn.cdzdjd.service.MeiTuanVideoService;
import cn.cdzdjd.service.OpenEyeService;
import cn.cdzdjd.service.PearVideoService;
import cn.cdzdjd.service.PipiGXService;
import cn.cdzdjd.service.PipixVideoService;
import cn.cdzdjd.service.TxWeiShiVideoService;
import cn.cdzdjd.service.UCService;
import cn.cdzdjd.service.UserSessionService;
import cn.cdzdjd.service.WeiBoService;
import cn.cdzdjd.service.XHSNoteVideoService;
import cn.cdzdjd.service.XinPianChangService;
import cn.cdzdjd.service.ZuiYouService;
import cn.cdzdjd.util.PlatformUrlProcess;
import cn.cdzdjd.util.Result;
import cn.cdzdjd.vendor.yuanxi.YuanXiApiService;
import cn.cdzdjd.vo.VideoInfoVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@RequestMapping("/api")
@Api(tags = "视频解析接口")
@RestController
@Slf4j
public class ShortVideoController {

	@Resource
	private DouyinVideoService douyinVideoService;
	@Resource
	private KuaiShouVideoService kuaishouVideoService;
	@Resource
	private XHSNoteVideoService noteVideoService;
	@Resource
	private PipixVideoService pipixVideoService;
	@Resource
	private TxWeiShiVideoService txWeiShiVideoService;
	@Resource
	private AllowURLService allowURLService;
	@Resource
	private ZuiYouService zuiYouService;
	@Resource
	private PipiGXService pipiGXService;
	@Resource
	private HuYaService huYaService;
	@Resource
	private PearVideoService pearVideoService;
	@Resource
	private XinPianChangService xinPianChangService;
	@Resource
	private AcfunService acfunService;
	@Resource
	private MeiPaiService meiPaiService;
	@Resource
	private BaiduHaoKanService baiduHaoKanService;
	@Resource
	private IXiGuaService ixiguaService;
	@Resource
	private BiliBiliService bilibiliService;
	@Resource
	private JDVideoService jdVideoService;
	@Resource
	private MeiTuanVideoService meiTuanVideoService;
	@Resource
	private DianPingService dianPingService;
	@Resource
	private UCService ucService;
	@Resource
	private WeiBoService weiBoService;
	@Resource
	private DuanYouService duanYouService;
	@Resource
	private OpenEyeService openEyeService;
	@Resource
	private UserSessionService userSessionService;
	@Resource
	private YuanXiApiService yuanXiApiService;

	@Log(value = "去水印")
	@Login
	@PostMapping("/transNoWaterMark")
	public Result<?> transNoWaterMark(@RequestBody UserInputForm form) {
		String url = PlatformUrlProcess.process(form.getUserInput()); // 解析出的url
		VideoInfoVO videoInfoVo = null;
		if (url == null) {
			return new Result<>().error("请输入带有链接的数据!");
		}
		// String locationUrl = LocationRedirect.getLocationMobile(url);
		// 如果是抖音平台
		if (url.contains(VideoPlatform.dy.getKey())) {
			String locationUrl = LocationRedirect.getLocationMobile(url);
			if (locationUrl.contains("/note/") || locationUrl.contains("/douyin/share/video/")) {
				if (locationUrl.contains("/note/")) {
					log.info("抖音图文");
				}
				if (locationUrl.contains("/douyin/share/video/")) {
					log.info("抖音中的西瓜视频");
				}
				videoInfoVo = douyinVideoService.getVideoInfoFromPage(locationUrl);
				inserURL(videoInfoVo);

				return new Result<>().ok(videoInfoVo);
			}
			if (locationUrl.contains("/video/") || locationUrl.contains("/slides/")) {
				videoInfoVo = douyinVideoService.getVideoInfoByApi(locationUrl);
				inserURL(videoInfoVo);
				log.info("抖音视频");
				return new Result<>().ok(videoInfoVo);
			}
		}
		if (url.contains(VideoPlatform.xhslink.getKey())) {
			String locationUrl = LocationRedirect.getLocationMobile(url);
			log.info("小红书");
			videoInfoVo = noteVideoService.getVideoInfo(locationUrl);
			inserURL(videoInfoVo);
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.kuaishou.getKey())) {
			videoInfoVo = kuaishouVideoService.getVideoInfo(url);
			inserURL(videoInfoVo);
			return new Result<>().ok(videoInfoVo);
		}

		if (url.contains(VideoPlatform.pipix.getKey())) {
			String locationUrl = LocationRedirect.getLocationMobile(url);
			videoInfoVo = pipixVideoService.getVideoInfo(locationUrl);
			inserURL(videoInfoVo);
			log.info("皮皮虾");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.txweishi.getKey())) {
			String locationUrl = LocationRedirect.getLocationMobile(url);
			videoInfoVo = txWeiShiVideoService.getVideoInfo(locationUrl);
			inserURL(videoInfoVo);
			log.info("腾讯微视");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.zuiyou.getKey())) {
			videoInfoVo = zuiYouService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("最右app");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.pipigx.getKey())) {
			videoInfoVo = pipiGXService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("皮皮搞笑");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.huya.getKey())) {
			videoInfoVo = huYaService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("虎牙");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.pear.getKey())) {
			videoInfoVo = pearVideoService.getVideoinfo(url);
			inserURL(videoInfoVo);
			log.info("梨视频");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.xinpianchang.getKey())) {
			videoInfoVo = xinPianChangService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("新片场");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.acfun.getKey())) {
			videoInfoVo = acfunService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("acfun");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.meipai.getKey())) {
			videoInfoVo = meiPaiService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("美拍");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.haokan.getKey())) {
			videoInfoVo = baiduHaoKanService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("百度好看");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.ixigua.getKey())) {
			videoInfoVo = ixiguaService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("西瓜视频");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.bilibili.getKey()) || url.contains(VideoPlatform.b23.getKey())) {
			videoInfoVo = bilibiliService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("哔哩哔哩");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.uc.getKey())) {
			videoInfoVo = ucService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("uc");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.dpurl.getKey()) || url.contains(VideoPlatform.dianping.getKey())
				|| url.contains(VideoPlatform.meituan.getKey())) {
			String locationUrl = LocationRedirect.getLocationMobile(url);
			if (locationUrl.contains(VideoPlatform.meituan.getKey())) {
				videoInfoVo = meiTuanVideoService.getVideoInfo(locationUrl);
				inserURL(videoInfoVo);
				log.info("美团");
				return new Result<>().ok(videoInfoVo);
			}
			if (locationUrl.contains(VideoPlatform.dianping.getKey())) {
				videoInfoVo = dianPingService.getVideoInfo(locationUrl);
				inserURL(videoInfoVo);
				log.info("大众点评");
				return new Result<>().ok(videoInfoVo);
			}
		}
		if (url.contains(VideoPlatform.weibo.getKey())) {
			videoInfoVo = weiBoService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("微博");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.duanyou.getKey())) {
			videoInfoVo = duanYouService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("段友");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.openeye.getKey())) {
			videoInfoVo = openEyeService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("开眼");
			return new Result<>().ok(videoInfoVo);
		}
		if (url.contains(VideoPlatform.tiktok.getKey())) {
			videoInfoVo = yuanXiApiService.getVideoInfo(url);
			inserURL(videoInfoVo);
			log.info("tiktok");
			return new Result<>().ok(videoInfoVo);
		}
		return new Result<>().error(ErrorCode.NOT_SUPPORT);
	}

	private void inserURL(VideoInfoVO videoInfo) {
		if (videoInfo != null) {
			Set<String> set = new HashSet<>();
			List<String> videoList = videoInfo.getVideoList();
			if (videoList != null && !videoList.isEmpty()) {
				videoList.forEach(item -> {
					try {
						URL url = new URL(item);
						String host = url.getHost();
						String realUrl = "https://" + host;
						set.add(realUrl);
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				});
			}
			List<String> posterList = videoInfo.getPosterList();
			if (posterList != null && !posterList.isEmpty()) {
				posterList.forEach(item -> {
					try {
						URL url = new URL(item);
						String host = url.getHost();
						String realUrl = "https://" + host;
						set.add(realUrl);
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				});
			}
			allowURLService.batchSave(set);
		}
	}
}
